'use strict';
// 营业员视图模块
var $iosDialog2, $merQrCodeDialog;
import {
    titileInputInit
} from '../components/titleInput.js';

var $userId, $userCard, $infoSubmit;
// 视图初始化
function viewInit() {
    $iosDialog2 = $("#iosDialog2");
    $merQrCodeDialog = $("#merQrCodeDialog");
    $userId = $("#user_id");
    $userCard = $("#user_card");
    $infoSubmit = $("#info_submit");
    titileInputInit();
    subscribeListData();
    hideInfoModel();
    onCloseErroEvent();
    bindViewEvent();
}

// 显示营业员页面功能
function showSignSuccess() {
    $("#qiandao").hide();
    $("#sign_success").css("display", "flex");
}

// 隐藏商户列表
function hideListDialong() {
    $("#listDialog").fadeOut(200);
}

// 监听取得商户列表弹出签到商户列表
function subscribeListData() {
    // 显示商户列表弹框
    $.subscribe("showListDialog", function(e, listData) {
        $("#listDialog").css({
            "opacity": "1",
            "display": ""
        });
        $("#listDialog").fadeIn(200);
        if (listData.merchant_Qw_response && listData.merchant_Qw_response.resp_code == "0000") {
            // 添加商户列表
            var mer_result = listData.merchant_Qw_response.mer_result_arrays.mer_result;
            var $merchanList = $("#merchanList");
            $merchanList.empty();

            for (var i = 0, l = mer_result.length; i < l; i++) {
                var oneHtml = createSHlist(mer_result[i]);
                $merchanList.append(oneHtml);
            }
        } else {
            hideListDialong();
            showErroDialog("签到已失效,请重新签到");
            $("#qiandao").text("签到").show();
            $("#sign_success").hide();
        }



    });
}


// 签到商户列表
function createSHlist(shdata) {

    var oneHtml = `
        <div data-mer_no=${shdata.merNo} class="weui-cell">
            <div class="weui-cell__bd">
                <p>${shdata.merName}</p>
            </div>
            <div class="weui-cell__ft"><img src="../images/tltd/erweima_icon.png" alt="" style="width:20px;margin-right:5px;display:block"></div>
        </div>
    `
    return oneHtml;
}


// 注册开通成功隐藏信息填写弹框
function hideInfoModel() {
    $.subscribe("registerEnd", function() {
        $iosDialog2.fadeOut(200);
    });
}

// 显示错误提示
function showErroDialog(erroMsg) {
    $("#errorDialog").css({
        "opacity": "1",
        "display": ""
    });

    $("#errorDialog .weui-dialog__bd").text(erroMsg);
}

// 隐藏错误提示
function hideErroDialog(erroMsg) {
    $("#errorDialog").fadeOut(200);

    //    $("#errorDialog .weui-dialog__bd").text(erroMsg);
}

// 显示错误提示2
function showErroDialog2(erroMsg) {
    $("#errorDialog2").css({
        "opacity": "1",
        "display": ""
    });

    $("#errorDialog2 .weui-dialog__bd").text(erroMsg);
}

// 隐藏错误提示2
function hideErroDialog2(erroMsg) {
    $("#errorDialog2").fadeOut(200);

    //    $("#errorDialog2 .weui-dialog__bd").text(erroMsg);
}


// 绑定关闭错误弹框
function onCloseErroEvent() {
    $("#closeErrorDialog").on("touchstart", hideErroDialog);
    $("#closeMerQrCode").on("touchend", function() {
        setTimeout(function() {
            hideMerQrCode();
        }, 200);
    });

    $("#openAgain").on("touchend", function() {
        hideErroDialog2();
        showDialog();
    });
    $("#openNext").on("touchend", function() {
        hideErroDialog2();
    });
}


// 第一次登入弹出信息填写框
function showDialog(argument) {
    // 判断第一登入
    $iosDialog2.css({
        "opacity": "1",
        "display": ""
    });
}

// 显示商户二维码
function showMerQrCode(ticket) {
    $merQrCodeDialog.css({
        "opacity": "1",
        "display": ""
    });
}


// 隐藏 商户二维码
function hideMerQrCode(ticket) {
    $merQrCodeDialog.fadeOut(200);
}

// 检查开户输入是否正确
function checkKHinput() {
    var userIdVal = $userId.val();
    var userCardVal = $userCard.val();
    var issfq = _isSFZNo(userIdVal);
    var isyhk = _isXYKNum(userCardVal);
    if (issfq && isyhk) {
        $infoSubmit.attr("class", "weui-btn weui-btn_primary");
    } else {
        $infoSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default");
    }

}

// 验证身份证格式
function _isSFZNo(card) {
    var iscarNum = true;
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) { //身份证输入不合法
        iscarNum = false;
    }

    return iscarNum;
}

// 验证信用卡格式
function _isXYKNum(sText) {
    if (sText.length >= 13 && sText.length <= 19) {
        var numStr2 = sText.match(/\d*/i)[0];
        return numStr2 == sText ? true : false;
    } else {
        return false;
    }
}



// 绑定事件
function bindViewEvent() {
    $("#fanhui").on("touchend", function() {
        location.href = LoginInit_URL;
    });
    $userId.on("input", function() {
        var userIdVal = $userId.val();
        userIdVal = userIdVal.replace("x", "X");
        userIdVal = userIdVal.replace("*", "X");
        $userId.val(userIdVal);
        if (userIdVal.length > 18) {
            userIdVal = userIdVal.substr(0, 18);
            $userId.val(userIdVal);
        }
        checkKHinput();
    });
    $userCard.on("input", function() {
        var userCardVal = $userCard.val();
        if (userCardVal.length > 19) {
            userCardVal = userCardVal.substr(0, 19);
            $userCard.val(userCardVal);
        }
        checkKHinput();
    });
    $("#info-code").on("touchend", function() {
        setTimeout(function() {
            $iosDialog2.fadeOut(200);
        }, 200);
    });
}

export {
    viewInit,
    showSignSuccess,
    showDialog,
    hideListDialong,
    hideErroDialog,
    showErroDialog,
    showMerQrCode,
    hideMerQrCode,
    showErroDialog2,
    hideErroDialog2


}